package com.lucher.mes.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lucher.mes.common.Tools;
import com.lucher.mes.dao.MaterialdepotMapper;
import com.lucher.mes.dao.MaterialinoutMapper;
import com.lucher.mes.dao.OperatorMapper;
import com.lucher.mes.dao.ProduceinfoMapper;
import com.lucher.mes.dao.ProduceproductMapper;
import com.lucher.mes.dao.ProductMapper;
import com.lucher.mes.dao.ProductdepotMapper;
import com.lucher.mes.dao.ProductinoutMapper;
import com.lucher.mes.po.Materialdepot;
import com.lucher.mes.po.Materialinout;
import com.lucher.mes.po.Operator;
import com.lucher.mes.po.Produceinfo;
import com.lucher.mes.po.Produceproduct;
import com.lucher.mes.po.Product;
import com.lucher.mes.po.Productdepot;
import com.lucher.mes.po.Productinout;

@Controller
@RequestMapping("/produceproduct")  
public class ProduceproductController extends BaseController{
	@Autowired
	private ProduceproductMapper produceproductDao;
	@Autowired
	private MaterialdepotMapper materialdepotDao;
	@Autowired
	private OperatorMapper operatorDao;
	@Autowired
	private MaterialinoutMapper materialinoutDao;
	@Autowired
	private ProductdepotMapper productdepotDao;
	@Autowired
	private ProductinoutMapper productinoutDao;
	@Autowired
	private ProduceinfoMapper produceinfoDao;
	
	/*
     * 特殊的instance变量
     * 注：零长度的byte数组对象创建起来将比任何对象都经济――查看编译后的字节码：
     * 生成零长度的byte[]对象只需3条操作码，而Object lock = new Object()则需要7行操作码。
     */
    private static byte[] lock = new byte[0];
	
	/**
	 * 查询信息
	 */
	@RequestMapping(value = "/findByList")
	public String findByList(Produceproduct form) {
		return "produceproduct/findProduceproductList";
	}
	
	/**
	 * 查询信息Json
	 */
	@ResponseBody
	@RequestMapping(value = "/findListJson")
	public Map<String, Object> findListJson(Produceproduct form) {
		//封装JSon的Map
		Map<String, Object> result = new HashMap<String, Object>(); 
		result.put("code", "0");
		List<HashMap<String, Object>> objectlist = new ArrayList<HashMap<String, Object>>();
		
		Integer total = produceproductDao.findByCount(form);
		List<Produceproduct> list = produceproductDao.findByListForPage(form);
		
		for (Produceproduct produceproduct : list) {
			HashMap<String, Object> objectMap = new HashMap<String, Object>();
			
			objectMap.put("id", produceproduct.getId());
			objectMap.put("producecode", produceproduct.getProducecode());
			
			Produceinfo produceinfo = produceinfoDao.findByProducecode(produceproduct.getProducecode());
			if(produceinfo != null){
				objectMap.put("linecode", produceinfo.getLinecode());
				objectMap.put("linename", produceinfo.getLinename());
			}
			
			objectMap.put("productcode", produceproduct.getProductcode());
			objectMap.put("productname", produceproduct.getProductname());
			objectMap.put("batchno", produceproduct.getBatchno());
			objectMap.put("depotamount", produceproduct.getDepotamount());
			objectMap.put("productstatus", produceproduct.getProductstatus());
			objectMap.put("printflag", produceproduct.getPrintflag());
			objectMap.put("addtime", StringUtils.isEmpty(produceproduct.getAddtime())?"":Tools.getStr(produceproduct.getAddtime()).substring(0, 10));
			objectMap.put("nfcid", produceproduct.getNfcid());
			objectMap.put("checkdata", StringUtils.isEmpty(produceproduct.getCheckdata())?"":Tools.getStr(produceproduct.getCheckdata()).substring(0, 19));
			objectMap.put("checkercode", produceproduct.getCheckercode());
			objectMap.put("checkername", produceproduct.getCheckername());
			objectMap.put("remark", produceproduct.getRemark());
			
			objectlist.add(objectMap);
		}
		
		result.put("count", total);
		result.put("data", objectlist);
		return result;
	}

	/**
	 * 保存产品入库
	 */
	@RequestMapping(value = "/saveIndepot")
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	public Map<String,Object> saveIndepot(Productdepot form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Operator operator = (Operator)getSession().getAttribute("Operator");
		String currenttime = Tools.getCurrentTime();
		Produceproduct produceproduct = produceproductDao.selectByPrimaryKey(form.getId());
		if(produceproduct == null){
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，该生产产品不存在");
			return responseJson;
		}
		if(!"生产完成".equals(produceproduct.getProductstatus())){
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，该生产产品不是生产完成状态，不能入库");
			return responseJson;
		}
		
		//修改生产产品的状态为已入库
		produceproduct.setProductstatus("已入库");
		produceproductDao.updateByPrimaryKeySelective(produceproduct);
		
		//产品库存增加
		Productdepot productdepot = new Productdepot();
		productdepot.setProductcode(produceproduct.getProductcode());
		productdepot.setProductname(produceproduct.getProductname());
		productdepot.setProductsource("自产");
		productdepot.setBatchno(produceproduct.getBatchno());
		productdepot.setDepotamount(produceproduct.getDepotamount());
		productdepot.setProductstatus("正常");
		productdepot.setCheckstatus("已检验");
		productdepot.setAddtime(currenttime);
		productdepot.setDepotstatus("有库存");
		productdepot.setOperatorcode(operator.getLoginname());
		productdepot.setInoutercode(operator.getLoginname());
		productdepotDao.insertSelective(productdepot);
		
		//增加产品入库记录
		Productinout productinout = new Productinout();
		productinout.setProductcode(produceproduct.getProductcode());
		productinout.setProductname(produceproduct.getProductname());
		productinout.setBatchno(produceproduct.getBatchno());
		productinout.setType("入库");
		productinout.setInoutercode(operator.getLoginname());
		productinout.setInoutamount(produceproduct.getDepotamount());
		productinout.setAddtime(currenttime);
		productinout.setOperatorcode(operator.getLoginname());
		productinout.setInoutercode(form.getInoutercode());
		productinout.setInoutstatus("已领");

		//入库记录保存
		productinoutDao.insertSelective(productinout);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
	
	/**
	 * 保存产品报废
	 */
	@RequestMapping(value = "/saveProductScrap")
	@ResponseBody //此标志就是返回jesion数据给页面的标志
	public Map<String,Object> saveProductScrap(Productdepot form) {
		
		//封装返回给页面的json对象
		HashMap<String,Object> responseJson = new HashMap<String,Object>();
		responseJson.put("code", "0");
		
		Operator operator = (Operator)getSession().getAttribute("Operator");
		String currenttime = Tools.getCurrentTime();
		Produceproduct produceproduct = produceproductDao.selectByPrimaryKey(form.getId());
		if(produceproduct == null){
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，该生产产品不存在");
			return responseJson;
		}
		if(!"待返工".equals(produceproduct.getProductstatus())){
			responseJson.put("flag", "0"); //操作失败
			responseJson.put("msg", "操作失败，该生产产品不是待返工状态，不能报废");
			return responseJson;
		}
		
		//修改生产产品的状态为报废
		produceproduct.setProductstatus("报废");
		produceproduct.setCheckdata(currenttime);
		
		produceproductDao.updateByPrimaryKeySelective(produceproduct);
		
		responseJson.put("flag", "1");//操作成功
		responseJson.put("msg", "操作成功");
		
		return responseJson;
	}
}
